PowerTCP Zip Compression for .NET
Data Decompression



The QuickUnzip method is nice for simple unzipping operations. For more sophisticated manipulation of an archive and its items, the Unzip method can be used. A compressed archive can be opened using the Open method. Individual items' properties, such as Password and Comment, can then be set. The items can then be extracted individually to a path or stream using the Unzip method of ArchiveItem. Or the archive as a whole can be extracted to a path using Unzip method of Archive.

Advanced unzipping a compressed file

  1. Add the Archive component to a new form.
  2. Add a button to the form.
  3. Open the zip file using Archive.Open.
  4. Specify the archive properties. For example, set the Password and Encryption properties to decrypt the entire collection. In the example to follow, all files in the zip archive "c:\test.zip" are extracted to the directory "c:\Test". The archived files' paths are preserved during decompression, and all existing files will be overwritten. Begin by placing the code below in the click event of the button added to the form in step 2.
  5. Use the Unzip method to extract files from the zip archive. Place the following code directly after the code from step 3.
  6. Compile and run the application. After pressing the button, the archived files contained in "c:\test.zip" will be extracted to the path "c:\Test".
C#
Copy Code
//Alternatively, a compressed stream could have been opened
archive1.Open("c:\\test.zip");
archive1.PreservePath = true;
archive1.Overwrite = Overwrite.Always;
archive1.Unzip("c:\\Test");
Visual Basic
Copy Code
'Alternatively, a compressed stream could have been opened
Archive1.Open("c:\test.zip")
Archive1.PreservePath = True
Archive1.Overwrite = Always
Archive1.Unzip("c:\Test")

Advanced unzipping to a stream

  1. Follow steps 1, 2 and 3 above, for unzipping a compressed file or stream.
  2. Use the Unzip method to extract individual items to a stream or path on disk.
  3. Compile and run the application. After pressing the button, the first file in the compressed archive should be found in "c:\Test", and the second within the memory stream.
C#
Copy Code
if (archive1.Count > 1)
{
    //Unzip the first item to a file, preserving its path
    archive1[0].Unzip("c:\\Test", true, Overwrite.Always);
    //Unzip the second item to a memory stream
    System.IO.MemoryStream stream = new System.IO.MemoryStream();
    archive1[1].Unzip(stream);
}
Visual Basic
Copy Code
If Archive1.Count > 1 Then
    'Unzip the first item to a file, preserving its path
    Archive1(0).Unzip("c:\Test", True, Overwrite.Always)
    'Unzip the second item to a memory stream
    Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream()
    Archive1(1).Unzip(stream)
End If

Unzipping an Archive asynchronously

  1. Follow steps 1, 2 and 3 above for advanced unzipping a compressed file.
  2. Use the asynchronous BeginQuickUnzip method to extract the archived files.
  3. Add code for the EndUnzip Event. When using the BeginXXX methods, you must have corresponding EndXXX events (see Asynchronous Operation for more details).
  4. Compile and run the application. After pressing the button, the archived files contained in "c:\test.zip" will be extracted to the path "c:\Test".
C#
Copy Code
archive1.BeginQuickUnzip("c:\\test.zip", "c:\\Test", null);
...
private void archive1_EndUnzip(object sender, Dart.PowerTCP.Zip.EndEventArgs e)
{
    try
    {
        if (e.Exception == null) MessageBox.Show("Extraction Complete!");
        else MessageBox.Show(e.Exception.Message);
    }
    catch {}
}
Visual Basic
Copy Code
Archive1.BeginQuickUnzip("c:\test.zip", "c:\Test", Nothing)
...
Private Sub Archive1_EndUnzip(ByVal sender As Object, ByVal e As Dart.PowerTCP.Zip.EndEventArgs) Handles Archive1.EndUnzip
    Try
        If (e.Exception Is Nothing) Then MessageBox.Show("Extraction Complete!")
        Else MessageBox.Show(e.Exception.Message)
        End If
    Catch
    End Try
End Sub

Unzipping an ArchiveItem asynchronously

  1. Follow the steps 1 and 3 above for unzipping an archive asynchronously. In place of step 2, use the following code.
  2. Compile and run the application. After pressing the button, the first archived file contained in "c:\test.zip" will be extracted to the path "c:\Test" and the MemoryStream will contain the second archived item.
C#
Copy Code
if (archive1.Count > 1)
{
    //Unzip the first item to a file, preserving its path
    archive1[0].BeginUnzip("c:\\Test", true, Overwrite.Always, null);
    //Unzip the second item to a memory stream
    System.IO.MemoryStream stream = new System.IO.MemoryStream();
    archive1[1].BeginUnzip(stream, null);
}
Visual Basic
Copy Code
If Archive1.Count > 1 Then
    'Unzip the first item to a file, preserving its path
    Archive1(0).BeginUnzip("c:\Test", True, Overwrite.Always, Nothing)
    'Unzip the second item to a memory stream
    Dim stream = New System.IO.MemoryStream()
    Archive1(1).BeginUnzip(stream, Nothing)
End If

PowerTCP Zip for .NET Documentation Version 2.1.1
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic